package org.hsc.proxy;
/*
 * HttpProxyServer.java
 *
 * Created on December 22, 2007
 *
 * Name:        Moty Michaely   |   Alon Halfon
 * ID:          043413087       |   300545969
 */

import java.io.*;

/**
 * A simple Http proxy server.
 * 
 * @author Moty Michaely
 */
public class HttpProxyServer extends TcpServer {
    
    public enum ConnectionType { New, Existing }
    
    /** The default socket timeout  */
    public static final int DefaultTimeout = 60000;  
    
    private volatile int maxFileSize=Integer.MAX_VALUE;
    
    /** the cache of the proxy  */
    private volatile Cache proxyCache;
    
    /** Holds the currently connected clients count.*/
    private volatile int clientsCount;
    
    private int ttl;
    
    /**
     * Creates a new instance of HttpProxyServer that listens for incoming client requests on port. s
     */
    public HttpProxyServer(int port, int cacheSize, int ttl){
        super(port);
        this.clientsCount = 0;
        this.proxyCache = new Cache();
        this.proxyCache.setMaxCacheSize(cacheSize);
        this.ttl = ttl;
    }
    
    /**
     * Handles the incoming socket connection.
     */
    protected synchronized void accept(java.net.Socket socket){
        final HttpProxyClientHandler handler = new HttpProxyClientHandler(socket,this);
        // Start processing http requests using an Http Proxy Handler
        Thread t = new Thread(new Runnable() {
            public void run() { 
                clientsCount++;
                handler.processRequests(); 
                clientsCount--;
            }
        });
        t.setDaemon(true);
        t.start();
    }
    
    /**
     * Starts the proxy server service. This makes sure a mediator is listening for managment 
     * messages regarding the proxy server.
     */
    public synchronized int getClientsCount(){
        return this.clientsCount;
    }
    
    /**
     * Main entry point.
     * 
     * @param args  The command line arguments
     */
    public static void main(String[] args) throws IOException{
            HttpProxyServer server = null;
            try{
                server = new HttpProxyServer(8083,1024,1000);
                System.out.println("Server started... Press any key to exit.");
                server.start();
                System.in.read();
                server.stop();
            }catch(Exception ioe){
                System.out.println("An error occured when starting to listen for incoming connections.\nError: "+ioe.getMessage());
            }
    }

	public synchronized void setMaxFileSize(int newMax){
        this.proxyCache.setMaxFileSize(newMax);
        this.maxFileSize=newMax;
    }
    
    public int getMaxFileSize(){
        return this.maxFileSize;
    }
    
    public Cache getCache(){
        return this.proxyCache;
    }
    
    public synchronized void start() throws IOException {
        // Start http proxy router.
        HttpProxyRouter.getInstance().start(this);
        //
        super.start();
    }
    
    public synchronized void stop() throws IOException {
        // Stop http proxy router.
        HttpProxyRouter.getInstance().stop();
        // Start the server
        super.stop();
    }

    public int getTtl() {
        return this.ttl;
    }
}
